/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.dao;

import java.util.List;
import nhadat.entity.TblLoaiGiayTo;
import nhadat.hibernate.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Xe Bay + DUNGND60190
 */
public class LoaiGiayToDAO {

    private Session session;

    public LoaiGiayToDAO() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

//    public static void main(String[] arg) {
//        LoaiGiayToDAO test = new LoaiGiayToDAO();
//        String value = test.getMaLoaiGiaytoByTenLoaiGiayto("Sổ đỏ");
//        System.out.println(value);
//    }

    public String getMaLoaiGiaytoByTenLoaiGiayto(String tenloaigiayto) {
        Transaction trans = null;
        try {
            trans = session.getTransaction();
            trans.begin();
            String sql = "select maloaigiayto from TblLoaiGiayTo where tenloaigiayto = ?";
            Query query = session.createQuery(sql);
            query.setString(0, tenloaigiayto);

            String maloaigiayto = query.uniqueResult().toString();

            session.flush();
            trans.commit();
            if (!maloaigiayto.isEmpty()) {
                return maloaigiayto;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

    public TblLoaiGiayTo[] getAllLoaiGiayTo() {
        Transaction trans = null;
        try {
            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblLoaiGiayTo";
            Query query = session.createQuery(sql);

            List result = query.list();

            TblLoaiGiayTo[] dsLoaiGiayTo = new TblLoaiGiayTo[result.size()];
            result.toArray(dsLoaiGiayTo);

            session.flush();
            trans.commit();

            return dsLoaiGiayTo;
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            // e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }
}
